package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import entity.User;
import serviceImpl.updateUserData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "PersonalityServlet", value = "/PersonalityServlet")
public class PersonalityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        User user=(User)request.getSession().getAttribute("user");
        System.out.println(user);
        String nickname=request.getParameter("nickname");
        String profile=request.getParameter("profile");
        String tag=request.getParameter("tag");
        String identity=request.getParameter("identity");
        String gender=request.getParameter("gender");
        String age=request.getParameter("age");

        user.setNickname(nickname);
        user.setGender(gender);
        user.setIdentity(identity);
        user.setProfile(profile);
        user.setTag(tag);
        user.setAge(Integer.parseInt(age));

        System.out.println(user);


        PrintWriter out=response.getWriter();
        updateUserData u=new updateUserData();
        ObjectMapper mapper = new ObjectMapper();
        int status = u.editUserData(user);
        if(status!=0){

            String json =  mapper.writeValueAsString(user);
            System.out.println(json.toString());
            out.print(json);
        }

    }
}
